Macromedia Flex Article

Learning Flex Basics (Part 4): Working with Data Models


Table of Contents

  1. Introduction
  2. Create the Application Object and Application Layout
  3. Add the Form Controls and the DataGrid Controls
  4. Add the New Employee Model, Employee, and Location Models
  5. Instantiate the DataController Class, and Create the Employee and DataController Classes
  6. Add the DataController Methods
  7. Code Listing for data.mxml
  8. Code Listings for DataController.as and Employee.as
  9. Code Listings for locations.xml and employee.xml
  10. Where to Go From Here

Create the Application Object

To begin any Flex application, start with an XML declaration and an Application tag. The Application tag will include a namespace declaration for the MX class library: xmlns:mx="http://www.macromedia.com/2003/mxml". The mx prefix is used with all tags for this library.

  1. Create a new file and save it as data.mxml in the flex_tutorials directory.
  2. At the beginning of the file, insert the XML declaration:

    <?xml version="1.0">
    
  3. After the XML declaration, add an Application tag with the MXML namespace:

    <mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">
    </mx:Application>
    

Create the Application Layout

You create the application layout using containers covered in Learning Flex Basics (Part 3): Working with Containers. Here, you use HBox, VBox, Panel, and Form containers to create a layout consisting of a main panel that contains a form, and two smaller panels placed to the right of the main panel.

  1. Inside the Application block, add an HBox container with no properties:

    <mx:HBox>
    </mx:HBox>
  2. Inside the HBox container, add a Panel container with the following properties:

    <mx:Panel id="empPanel" title="Employee Data" height="{engPanel.height+supPanel.height}">
    </mx:Panel>
    

    Note: Bind this panel height to the the sum of the heights of two panels you will create beside this one—this maintains the proportions of the application without hard coding the heights. You will bind the widths of the two panels on the right to this one in the same way later.

  3. Inside the Panel, add a Form container with no properties:

    <mx:Form>
    </mx:Form>
    
  4. After the Form container, but still inside the Panel container, add a ControlBar container with no properties:

    <mx:ControlBar>
    </mx:ControlBar>
  5. After the Panel container, add a VBox container with no properties:

    <mx:VBox>
    </mx:VBox>
    
  6. Inside the VBox container, add another Panel with the properties as shown:

    <mx:Panel id="engPanel" title="Engineering Group" width="{empPanel.width}">
    </mx:Panel>
    
  7. Still inside the VBox container, but after the Engineering Group Panel, add another Panel container with the properties as shown:

    <mx:Panel id="supPanel" title="Support Group" width="{empPanel.width}">
    </mx:Panel>